﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using FRL.DebtChased.Website;
using FRL.DebtChased.Website.FinanceParts;

namespace WebApplication.Finance
{

	/// <summary>
	/// Declaration of Dashboard class
	/// </summary>
	public partial class Dashboard : System.Web.UI.Page
	{

		/////////////////////////////////////////////////////////////
		// Events Handler

		protected void Page_Load(object sender, EventArgs e)
		{

			// If this is a postback...
			if (this.IsPostBack)
			{

			}
			else
			{

				// Get my session
				MySession mySession = MySession.BindMySession();

				// Fake login
#if DEBUG
				if (mySession.UserID == 0)
					mySession.SetDummySysAdmin();
#endif // #if DEBUG

				// If in editing mode...
				if (WebPartManager.DisplayMode == WebPartManager.DesignDisplayMode)
				{

				}
				else
				{

				}

				// Setup controls
				_setupControls();

			}

		}

		protected void butEditParts_Click(object sender, EventArgs e)
		{

			// Switch to design mode
			WebPartManager.DisplayMode = WebPartManager.EditDisplayMode;

			// Setup controls
			_setupControls();

		}

		protected void butLockParts_Click(object sender, EventArgs e)
		{

			// Switch to design mode
			WebPartManager.DisplayMode = WebPartManager.BrowseDisplayMode;

			// Setup controls
			_setupControls();

		}

		protected void butResetParts_Click(object sender, EventArgs e)
		{

			// Remove all parts
			foreach (WebPart p in WebPartManager.WebParts)
				WebPartManager.DeleteWebPart(p);

			// Setup default layout
			WebPartManager.AddWebPart(new FinWorkSummaryTable(), LeftVertZone, 0);
			WebPartManager.AddWebPart(new FinCollectorAlertsTable(), LeftVertZone, 1);
			WebPartManager.AddWebPart(new FinHitListTable(), LeftVertZone, 2);

			WebPartManager.AddWebPart(new FinInboxTable(), RightVertZone, 0);
			WebPartManager.AddWebPart(new FinPinkAccountsTable(), RightVertZone, 1);
			WebPartManager.AddWebPart(new FinQueriesAddressedTable(), RightVertZone, 2);
			WebPartManager.AddWebPart(new FinAccountsReviewedTable(), RightVertZone, 3);

			WebPartManager.AddWebPart(new FinCommissionTable(), BottomHorzZone, 0);

		}

		protected void PartsMenu_MenuItemClick(object sender, MenuEventArgs e)
		{

			// Compare menu clicks
			WebPart newPart = null;
			if (e.Item.Value == "WorkSummaryTable")
				newPart = new FinWorkSummaryTable();
			else if (e.Item.Value == "CollectorAlerts")
				newPart = new FinCollectorAlertsTable();
			else if (e.Item.Value == "PinkAccountsTable")
				newPart = new FinPinkAccountsTable();
			else if (e.Item.Value == "InboxTable")
				newPart = new FinInboxTable();
			else if (e.Item.Value == "QueriesAddressedTable")
				newPart = new FinQueriesAddressedTable();
			else if (e.Item.Value == "AccountsReviewedTable")
				newPart = new FinAccountsReviewedTable();
			else if (e.Item.Value == "HitListTable")
				newPart = new FinHitListTable();
			else if (e.Item.Value == "CommissionTable")
				newPart = new FinCommissionTable();

			// If there is a new part...
			if (newPart != null)
			{

				// Add to a zone with something already in it
				// so as not to upset the user's layout too much
				WebPartZoneBase targetZone = null;
				foreach (WebPartZone zone in WebPartManager.Zones)
				{
					if (zone.WebParts.Count > 0)
					{
						targetZone = zone;
						break;
					}
				}
				if (targetZone == null)
					targetZone = LeftVertZone;
				WebPartManager.AddWebPart(newPart, targetZone, targetZone.WebParts.Count);

			}

		}

		protected void WebPartManager_WebPartClosed(object sender, WebPartEventArgs e)
		{
			
			// remove the closed webpart
			WebPartManager.DeleteWebPart(e.WebPart);

		}

		/////////////////////////////////////////////////////////////
		// Operations

		private void _setupControls()
		{

			// Show/hide controls
			pnlEdit.Visible = (WebPartManager.DisplayMode != WebPartManager.BrowseDisplayMode);
			PartsMenu.Visible = (WebPartManager.DisplayMode != WebPartManager.BrowseDisplayMode);

			// Enable/disable the buttons
			butEditParts.Visible = (WebPartManager.DisplayMode == WebPartManager.BrowseDisplayMode);
			butLockParts.Visible = (WebPartManager.DisplayMode != WebPartManager.BrowseDisplayMode);
			butResetParts.Visible = (WebPartManager.DisplayMode == WebPartManager.BrowseDisplayMode);

		}

	}

}